/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *   Sun icons from Icons-Land at http://www.icons-land.com/vista-weather-icons.php
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

// Hack required because there is no way to replicate dynamic widget content to new windows (SDK 1.0b5).
self.port.emit('newWindow');

// Insert the string to be displayed, then determine and emit it's length in pixels.
// Also add/remove font and color properties.
self.port.on('insertTextAndMeasure', function(aText, aOptions, aWarnings) {
    var displayString = document.getElementById('text');
    var image = document.getElementById('image');
    image.hidden = true;

    // Font properties.
    if ( aOptions.toolbarStyle.bold ) {
        displayString.style.setProperty('font-weight', 'bold', '');
    }
    else {
        displayString.style.removeProperty('font-weight');
    }

    if ( aOptions.toolbarStyle.italic ) {
        displayString.style.setProperty('font-style', 'italic', '');
    }
    else {
        displayString.style.removeProperty('font-style');
    }

    // Color properties.
    var activeEventIndex = 0;

    if ( aOptions.toolbarColorEnabled && aWarnings.length > 0 ) {
        // Set colors based on most recent, and active, event. There may not be an active event.
        if ( aWarnings[1].type == 'Ended' ) {
            // Perhaps another event that hasn't ended yet.
            for ( let i = 2; i < aWarnings.length; i++ ) {
                if ( aWarnings[1].description != aWarnings[i].description &&
                     aWarnings[i].type != 'Ended' ) {
                    activeEventIndex = i;
                }
            }
        }
        else {
            activeEventIndex = 1;
        }
    }

    displayString.style.removeProperty('color');
    displayString.style.removeProperty('background-color');

    if ( activeEventIndex ) {
        switch ( aWarnings[activeEventIndex].priority ) {
            case 'Low':
                if ( aOptions.toolbarStyle.color.priorityLow[0] ) {
                    displayString.style.setProperty('color', aOptions.toolbarStyle.color.priorityLow[0], '');
                }

                if ( aOptions.toolbarStyle.color.priorityLow[1] ) {
                    displayString.style.setProperty('background-color', aOptions.toolbarStyle.color.priorityLow[1], '');
                }

                break;

            case 'Medium':
                if ( aOptions.toolbarStyle.color.priorityMedium[0] ) {
                    displayString.style.setProperty('color', aOptions.toolbarStyle.color.priorityMedium[0], '');
                }

                if ( aOptions.toolbarStyle.color.priorityMedium[1] ) {
                    displayString.style.setProperty('background-color',aOptions.toolbarStyle.color.priorityMedium[1], '');
                }

                break;

            case 'High':
                if ( aOptions.toolbarStyle.color.priorityHigh[0] ) {
                    displayString.style.setProperty('color', aOptions.toolbarStyle.color.priorityHigh[0], '');
                }

                if ( aOptions.toolbarStyle.color.priorityHigh[1] ) {
                    displayString.style.setProperty('background-color', aOptions.toolbarStyle.color.priorityHigh[1], '');
                }

                break;

            case 'Urgent':
                if ( aOptions.toolbarStyle.color.priorityUrgent[0] ) {
                    displayString.style.setProperty('color', aOptions.toolbarStyle.color.priorityUrgent[0], '');
                }

                if ( aOptions.toolbarStyle.color.priorityUrgent[1] ) {
                    displayString.style.setProperty('background-color', aOptions.toolbarStyle.color.priorityUrgent[1], '');
                }

                break;

            default:
                self.port.emit('unexpectedPriorityError', aWarnings[activeEventIndex].priority);
                break;
        }
    }

    // Clear existing text node.
    while ( displayString.firstChild ) {
        displayString.removeChild(displayString.firstChild);
    }

    displayString.appendChild(document.createTextNode(aText));
    self.port.emit('width', displayString.offsetWidth);
});

// Show the image.
self.port.on('showImage', function() {
    var displayString = document.getElementById('text');
    var image = document.getElementById('image');

    // Clear existing text node.
    while ( displayString.firstChild ) {
        displayString.removeChild(displayString.firstChild);
    }

    image.hidden = false;
    self.port.emit('width', 16);
});

// Listen for click events and emit the user-defined event.
// to the add-on code.
this.addEventListener('click', function(aEvent) {
    if( aEvent.button == 0 && aEvent.shiftKey == false ) {
        self.port.emit('left-click');
    }
    else if( aEvent.button == 2 || ( aEvent.button == 0 && aEvent.shiftKey == true ) ) {
        self.port.emit('right-click');
    }

    aEvent.preventDefault();
}, true);